<template>
  <div id="login">
    <div class="formbox">
      <img src="../assets/img/pic/logo/orange.png" alt="" class="logo" />
      <input type="text" placeholder="手机号" class="text" v-model="inputTel" />
      <input type="text" placeholder="密码" class="text" v-model="inputPw" />
      <button class="btn" @click="dologin" :class="flag">登录</button>
    </div>
  </div>
</template>

<script>
import { Dialog } from "vant";
import { Login } from "../util/request/api";
export default {
  data() {
    return {
      inputTel: "",
      inputPw: "",
      flag: "",
    };
  },
  watch: {
    // 判断改变登录按钮颜色
    inputPw: function () {
      this.onchange();
    },
    inputTel: function () {
      this.onchange();
    },
  },
  methods: {
    dologin() {
      if (this.inputTel == "" || this.inputPw == "") {
        Dialog({ message: "用户名密码不能为空" });
        return;
      }
      // axios
      Login({
        phone: this.inputTel,
        password: this.inputPw,
      })
        .then((res) => {
          if (res.code != 200) {
            this.$notify(res.msg);
            if (res.msg == "用户信息不存在") {
              this.$router.replace("/register");
            }
            return;
          }
          sessionStorage.setItem("user", JSON.stringify(res.list));
          this.$router.push("/home");
        })
        .catch((err) => {
          console.log(err);
        });
    },
    onchange() {
      if (this.inputTel != "" && this.inputPw != "") {
        this.flag = "active";
      } else {
        this.flag = "";
      }
    },
  },
};
</script>

<style scoped>
@import url("../assets/css/login.css");
</style>